package com.xls.commonlibrary.ad.adn.gm;

import androidx.appcompat.app.AppCompatActivity;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.mediation.IMediationPreloadRequestInfo;
import com.bytedance.sdk.openadsdk.mediation.MediationPreloadRequestInfo;
import com.orhanobut.logger.Logger;
import com.xls.commonlibrary.bean.AdCodeResult;

import java.util.ArrayList;
import java.util.List;

// 聚合SDK提供广告的预缓存功能
public class GMPreloadUtil {
    private static volatile GMPreloadUtil singleton = null;

    private GMPreloadUtil() {
    }

    public static GMPreloadUtil getInstance() {
        if (singleton == null) {
            synchronized (GMPreloadUtil.class) {
                if (singleton == null) {
                    singleton = new GMPreloadUtil();
                }
            }
        }
        return singleton;
    }

//    聚合SDK提供广告的预缓存功能：在合适时机(广告展示或关闭)来进行当前广告位的预加载，以满足媒体的使用场景。
//    https://www.csjplatform.com/union/media/union/download/detail?id=142&docId=27619&osType=android
    ////////////////////////////////////////////////////


    public void mediationPreloadAds(AppCompatActivity activity, ArrayList<AdCodeResult> adsJH
            , ArrayList<AdCodeResult> adsBP) {
        Logger.d("====gm===preload==============start=====");

        IMediationPreloadRequestInfo rewardPreloadInfo = null;
        if (adsJH!=null&&adsJH.size()>0) {
            //第一类: 准备激励视频预请求信息
            AdSlot rewardAdSlot = new AdSlot.Builder().build();
            List<String> rewardPrimeRitList = new ArrayList<>();
            for (AdCodeResult ad : adsJH) {
                rewardPrimeRitList.add(ad.getAd_code());
            }
            rewardPreloadInfo =
                    new MediationPreloadRequestInfo(AdSlot.TYPE_REWARD_VIDEO, rewardAdSlot, rewardPrimeRitList);
        }


        IMediationPreloadRequestInfo fullPreloadInfo = null;
        if (adsBP!=null&&adsBP.size()>0) {
            // 第二类 插屏
            AdSlot fullAdSlot = new AdSlot.Builder().build();
            List<String> fullPrimeRitList = new ArrayList<>();
            for (AdCodeResult ad : adsBP) {
                fullPrimeRitList.add(ad.getAd_code());
            }
            fullPreloadInfo =
                    new MediationPreloadRequestInfo(AdSlot.TYPE_FULL_SCREEN_VIDEO, fullAdSlot, fullPrimeRitList);
        }

///      /第二类: 准备信息流预请求信息
//        AdSlot feedAdSlot = new AdSlot.Builder().build();
//        List<String> feedPrimeRitList = new ArrayList<>();
//        feedPrimeRitList.add("信息流聚合广告位ID1");
//        feedPrimeRitList.add("信息流聚合广告位ID2");
//        IMediationPreloadRequestInfo feedPreloadInfo = new MediationPreloadRequestInfo(AdSlot.TYPE_FEED, feedAdSlot, feedPrimeRitList);

        // add all List
        List<IMediationPreloadRequestInfo> requestInfoList = new ArrayList<>();
        if (rewardPreloadInfo != null) {
            requestInfoList.add(rewardPreloadInfo);// 1
        }
        if (fullPreloadInfo != null) {
            requestInfoList.add(fullPreloadInfo);// 2
        }
        if (requestInfoList.size()>0){
            Logger.d("====gm===preload==============end=====");
            TTAdSdk.getMediationManager().preload(activity, requestInfoList, 2, 2);
        }
    }
}


//    参数
//            说明
//    Activity activity
//    上下文
//            preloadRequestInfoList
//    预加载广告位list
//            parallelNum
//    并行加载的广告位数，默认值为2，合法值为[1,20]
//    requestIntervalS
//    时间间隔，默认值为2s，合法值为[1,10]
